﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CardsServer.Tools
{
    public static class MyConfig
    {
        /// <summary>
        /// 得到APPConfig设置
        /// </summary>
        /// <param name="key">keyname</param>
        /// <returns></returns>
        public static string getAppValue(string key)
        {
            System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None);
            if (config.AppSettings.Settings[key] != null)
            {
                return config.AppSettings.Settings[key].Value;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 设置Appconfig值
        /// </summary>
        /// <param name="key">keyname</param>
        /// <param name="value">keyvalue</param>
        /// <returns>0=失败 1=成功</returns>
        public static int? setAppValue(string key, string value)
        {
            System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None);

            int saveTag = 0;
            if (config.AppSettings.Settings[key] != null)
            {
                config.AppSettings.Settings[key].Value = value;
                saveTag = 0;
            }
            else
            {
                config.AppSettings.Settings.Add(key, value);
                saveTag = 1;
            }


            config.Save(System.Configuration.ConfigurationSaveMode.Modified);
            return saveTag;

        }

        /// <summary>
        /// 设置连接字符串属性
        /// </summary>
        /// <param name="conKey">连接字符串name</param>
        /// <param name="dataSource">服务器</param>
        /// <param name="initialCatalog">数据库名称</param>
        /// <param name="uid">数据库用户</param>
        /// <param name="pass">密码</param>
        /// <returns>0=失败 1=成功</returns>
        public static int? setConnection(string server, string database, string uid, string pwd)
        {

            if (string.IsNullOrEmpty(server)) return null;
            if (string.IsNullOrEmpty(database)) return null;
            if (string.IsNullOrEmpty(uid)) return null;
            int saveTag = 0;


            System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None);
            try
            {
                setAppValue("server", server);
                setAppValue("database", database);
                setAppValue("user id", uid);
                setAppValue("pwd", pwd);

                saveTag = 1;
            }
            catch
            {
                saveTag = 0;
            }
            return saveTag;
        }



        /// <summary>
        /// 得到连接字符串值
        /// </summary>
        /// <param name="conKey">连接字符串名称</param>
        /// <returns></returns>
        public static sqlconn getConnection()
        {
            //帐套数据库连接字符串
            sqlconn sc = new sqlconn();
            sc.server = MyConfig.getAppValue("server");
            sc.database = MyConfig.getAppValue("database");
            sc.uid = MyConfig.getAppValue("user id");
            sc.pwd = MyConfig.getAppValue("pwd");
            sc.connstr = string.Format("server={0};database={1};user id={2};pwd={3}", sc.server, sc.database, sc.uid, sc.pwd);
            return sc;
        }
    }
}
